#include "clienthandler.h"

#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "../common/singleton.h"
#include "../common/streamer.h"
#include "client.h"
#include "loginwidget.h"
#include "hallwidget.h"
#include "anchorroom.h"
#include "userinfo.h"
#include <QMessageBox>
#include "audioout.h"


ClientHandler::ClientHandler(Client* client)
{
    handlers = QVector<IHandler*>(last, nullptr);

    handlers[regist] = new ClientHandlerRegister(client);
    handlers[login] = new ClientHandlerLogin(client);
    handlers[chat] = new ClientHandlerChat(client);
    handlers[createRoom] = new ClientHandlerCreateRoom(client);
    handlers[joinRoom] = new ClientHandlerJoinRoom(client);
    handlers[closeRoom] = new ClientHandlerCloseRoom(client);
    handlers[getRoom] = new ClientHandlerGetRoom(client);
    handlers[addAudience] = new ClientHandlerAddAudi(client);
    handlers[deleAudience] = new ClientHandlerDeleAudi(client);
    handlers[audioTrans] = new ClientHandlerAudioTrans(client);
    handlers[videoTrans] = new ClientHandlerVideoTrans(client);
    handlers[heartbeat] = new ClientHandlerHeartCheck(client);
}

//注册-----------------------------------------
//本地操作
void ClientHandlerRegister::handle(const QByteArray& data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString result = json["result"].toString();

    if(result == "success"){
        // 成功
        QMessageBox::information(SingleTon<LoginWidget>::getInstance(), "注册信息", "注册成功");
        SingleTon<LoginWidget>::getReference().insertUser();
    }else if(result == "failed"){
        // 失败
        QMessageBox::information(SingleTon<LoginWidget>::getInstance(), "注册信息", "注册失败");
    }

}
//发送数据
void ClientHandlerRegister::transfer(const QString& username, const QString& password)
{
    QJsonObject json;
    json["username"] = username;
    json["password"] = password;

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::regist, data));
}

//登陆-----------------------------------------
//本地操作
void ClientHandlerLogin::handle(const QByteArray& data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString result = json["result"].toString();
    QString info = json["info"].toString();

    if(result == "success"){
        // 切换界面
        SingleTon<LoginWidget>::getReference().close();
        SingleTon<hallWidget>::getReference().show();
        SingleTon<hallWidget>::getReference().getrooms();

    }else if(result == "failed"){
        QMessageBox::information(SingleTon<LoginWidget>::getInstance(), "登录信息", info);
    }

}
//发送数据
void ClientHandlerLogin::transfer(const QString& username, const QString& password)
{
    QJsonObject json;
    json["username"] = username;
    json["password"] = password;
    auto data = QJsonDocument(json).toJson();
    SingleTon<Client>::getReference().write(Streamer::pack(Handler::login, data));

    //将用户姓名加入userinfo中
    SingleTon<UserInfo>::getReference().setUsername(username);
}

//聊天-----------------------------------------
void ClientHandlerChat::handle(const QByteArray& data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString username = json["username"].toString();
    QString message = json["message"].toString();

    QString text = username + " : " + message;

    SingleTon<AnchorRoom>::getReference().appendText(text);
    SingleTon<AnchorRoom>::getReference().appendbarrage(message);
}

void ClientHandlerChat::transfer(const QString& message)
{
    QJsonObject json;
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    json["message"] = message;

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::chat, data));
}

//创建房间------------------------------------
//本地操作
void ClientHandlerCreateRoom::handle(const QByteArray &data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString result = json["result"].toString();
    QString info = json["info"].toString();
    QString roomid = json["roomid"].toString();
    QString anchor = json["anchor"].toString();

    if(result == "success"){
        //设置现在属于的房间号
        SingleTon<UserInfo>::getReference().setCurRoom(roomid);
        // 切换界面
        SingleTon<hallWidget>::getReference().hide();
        SingleTon<AnchorRoom>::getReference().createRoom( anchor,  roomid);

    }else if(result == "failed"){
        QMessageBox::information(SingleTon<LoginWidget>::getInstance(), "创建房间", info);
    }
}
//发送数据
void ClientHandlerCreateRoom::transfer(const QString &roomid)
{
    QJsonObject json;
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    json["roomid"] = roomid;

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::createRoom, data));
}

//加入房间-----------------------------------------
void ClientHandlerJoinRoom::handle(const QByteArray &data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString roomid = json["roomid"].toString();
    QString anchor = json["anchor"].toString();
    QString result = json["result"].toString();

    if(result=="failed"){
        QMessageBox::information(SingleTon<hallWidget>::getInstance(), "joinRoom", "please select room");
        return;
    }
    SingleTon<UserInfo>::getReference().setCurRoom(roomid);

    // 切换界面
    SingleTon<hallWidget>::getReference().hide();
    SingleTon<AnchorRoom>::getReference().joinRoom(anchor,roomid);

    QJsonArray Audilist = json["list"].toArray();
    for (auto i : Audilist) {
        SingleTon<AnchorRoom>::getReference().addAudience(i.toString());
    }
}

void ClientHandlerJoinRoom::transfer(const QString &roomid)
{
    QJsonObject json;
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    json["roomid"] = roomid;

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::joinRoom, data));
}

//关闭房间-----------------------------------------
//本地操作
void ClientHandlerCloseRoom::handle(const QByteArray &data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();

    QString result = json["result"].toString();
    QString info = json["info"].toString();

    if(result == "off"){
        SingleTon<UserInfo>::getReference().setcloseable();
        QMessageBox::information(SingleTon<AnchorRoom>::getInstance(), "offline", info);
        // 切换界面
        SingleTon<AnchorRoom>::getReference().close();
        //SingleTon<hallWidget>::getReference().show();
    }
}
//发送数据
void ClientHandlerCloseRoom::transfer()
{
    QJsonObject json;
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    //json["roomid"] = SingleTon<UserInfo>::getReference().getCurRoom();

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::closeRoom, data));
}

//获取房间列表-----------------------------------------
//本地操作
void ClientHandlerGetRoom::handle(const QByteArray &data)
{
    //清空列表
    SingleTon<hallWidget>::getReference().clearrooms();

    QJsonObject json = QJsonDocument::fromJson(data).object();

    if(json["result"]=="no rooms"){return;}
        json.remove("result");
        QStringList list = json.keys();
        for(QString s : list)
        SingleTon<hallWidget>::getReference().addroom(s,json[s].toString());
        SingleTon<hallWidget>::getReference().showrooms();
}
//发送数据
void ClientHandlerGetRoom::transfer()
{

    QJsonObject json;
    json["room"]="room";

    auto data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::getRoom, data));
}

//添加观众-----------------------------------------
void ClientHandlerAddAudi::handle(const QByteArray &data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();
    QString audi = json["username"].toString();
    SingleTon<AnchorRoom>::getReference().addAudience(audi);

}

void ClientHandlerAddAudi::transfer()
{
    QJsonObject json = QJsonObject();
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    //json["roomid"] = roomid;
    QByteArray data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::addAudience,data));
}

//观众退出-----------------------------------------
void ClientHandlerDeleAudi::handle(const QByteArray &data)
{
    QJsonObject json = QJsonDocument::fromJson(data).object();
    QString audi = json["username"].toString();
    SingleTon<AnchorRoom>::getReference().deleAudience(audi);
}

void ClientHandlerDeleAudi::transfer()
{
    QJsonObject json = QJsonObject();
    json["username"] = SingleTon<UserInfo>::getReference().getUsername();
    //json["roomid"] = roomid;
    QByteArray data = QJsonDocument(json).toJson();

    SingleTon<Client>::getReference().write(Streamer::pack(Handler::deleAudience,data));
}

//音频输出-----------------------------------------
void ClientHandlerAudioTrans::handle(const QByteArray &data)
{
    SingleTon<audioOut>::getReference().play_data(data);
}

//视频处理-----------------------------------------
void ClientHandlerVideoTrans::handle(const QByteArray &data)
{
    SingleTon<AnchorRoom>::getReference().showCamera(data);
}
void ClientHandlerVideoTrans::transfer(const QByteArray &data)
{
    SingleTon<Client>::getReference().write(Streamer::pack(Handler::videoTrans,data));
}

void ClientHandlerHeartCheck::handle(const QByteArray &data)
{
    Q_UNUSED(data);
}
